package com.utils;

import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.bean.Msg;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;

/**
 * 异常返回类
 * @author arthorn
 * @create 2022-04-27-19:34
 */
@RestControllerAdvice
public class ProjectExceptionAdvice {
    @ExceptionHandler
    public R doException(Exception e){
        e.printStackTrace();
        return new R("服务器异常，请及时通知客服!");
    }

    /**
     * 相同的key插入异常
     * @param e
     * @return
     */
    @ExceptionHandler
    public Msg DuplicateKeyException(DuplicateKeyException e){
        return Msg.fail().add("result", "该用户已存在，请直接登录");
    }

    /**
     * 登录邮箱输入错误异常
     * @param e
     * @return
     */
    @ExceptionHandler
    public Msg UnknownAccountException(UnknownAccountException e){
        return Msg.fail().add("result", "邮箱输入错误");
    }

    /**
     * 登录密码输入错误异常
     * @param e
     * @return
     */
    @ExceptionHandler
    public Msg IncorrectCredentialsException(IncorrectCredentialsException e){
        return Msg.fail().add("result", "密码输入错误");
    }
    

    @ExceptionHandler
    public Msg ExpiredCredentialsException(ExpiredCredentialsException e){
        return Msg.fail().add("result","token过期，请重新登录");
    }


    @ExceptionHandler
    public Msg AuthenticationException(AuthenticationException e){
        return Msg.fail().add("result",e.getMessage());
    }

    @ExceptionHandler
    public Msg UnauthorizedException(UnauthorizedException e){
        return Msg.fail().add("result","该用户权限不够");
    }




    @ExceptionHandler
    public Msg LockedAccountException(LockedAccountException e){
        return Msg.fail().add("result",e.getMessage());
    }

    @ExceptionHandler
    public Msg    TokenExpiredException(   JWTVerificationException e){
        return Msg.fail().add("result",e.getMessage());
    }

}
